﻿/************************************************************************************************************
* file    : Channelfollowing.cs
* author  : RDSSH
* function: 
* history : created by RDSSH 2018/11/13 16:23:06
************************************************************************************************************/
using IStrong.EC.Web.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IStrong.EC.WebApi.Results.Entity;
using IStrong.EC.WebApi.Results.Repository;

namespace IStrong.EC.WebApi.Results.Controllers
{
    /// <summary>
    /// Api控制器模板
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ChannelfollowingController : BaseController
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ChannelfollowingController> _logger;

        /// <summary>
        /// 配置
        /// </summary>
        private readonly IConfiguration _config;


        private readonly ChannelfollowingRepository _channelfollowingRepository;

        /// <summary>
        /// 注入服务 
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="logger"></param>
        /// <param name="config"></param>
        public ChannelfollowingController(ILogger<ChannelfollowingController> logger, IConfiguration config, ChannelfollowingRepository channelfollowingRepository)
        {
            _logger = logger;
            _config = config;
            _channelfollowingRepository = channelfollowingRepository;
        }

        /// <summary>
        /// GET api/values
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult Get(int ChannelId, int PageSize = 50, int PageIndex = 1)
        {
            try
            {
                var queryParam = new
                {
                    ChannelId,
                    PageSize,
                    PageIndex
                };

                var data = _channelfollowingRepository.GetPaginList(queryParam).OrderByDescending(d => d.FollowingTime)
                    .Select(d => new
                    {
                        d.Id,
                        TypeName = ((ChannelTypeEnums)d.Type).GetDescription(),
                        d.FollowingContent,
                        d.FollowingUser,
                        d.FollowingTime,
                        d.CreatorUser,
                        d.Creationtime
                    }); 

                var totalCount = _channelfollowingRepository.GetTotalCount(queryParam).FirstOrDefault();

                var result = new
                {
                    pageSize = PageSize,
                    pageIndex = PageIndex,
                    totalCount,
                    data
                };

                return Json(result);

            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }

        /// <summary>
        /// GET api/values/5
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetById")]
        public JsonResult Get(int id)
        {
            try
            {
                var data = _channelfollowingRepository.GetChannelfollowingById(new { id });
                return Json(data);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }

        [HttpGet("All")]
        public JsonResult GetAll(int ChannelId)
        {
            try
            {
                var queryParam = new
                {
                    ChannelId
                };

                var data = _channelfollowingRepository.GetAll(queryParam).OrderByDescending(d => d.FollowingTime)
                    .Select(d=>new
                    {
                        d.Id,
                        TypeName = ((ChannelTypeEnums)d.Type).GetDescription(),
                        d.FollowingContent,
                        d.FollowingUser,
                        d.FollowingTime,
                        d.CreatorUser,
                        d.Creationtime
                    });

                return Json(data);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }

        /// <summary>
        /// POST api/values
        /// </summary>
        /// <param name="value"></param>
        [HttpPost]
        public JsonResult Post(Channelfollowing model)
        {
            try
            {
                var data = _channelfollowingRepository.AddChannelfollowing(model) > 0 ? true : false;

                return Json(data);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }

        /// <summary>
        /// PUT api/values/5
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        /// <summary>
        /// DELETE api/values/5
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete]
        public JsonResult Delete(int id, string deleteruserid, DateTime deletiontime)
        {
            try
            {
                var data = _channelfollowingRepository.DeleteChannelfollowing(new { id, deleteruserid, deletiontime }) > 0 ? true : false;
                return Json(data);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }
    }
}
